package com.jellyfishumbrella;

import java.util.ArrayList;

import be.humphreys.simplevoronoi.GraphEdge;

public class Corner implements Comparable {
	public ArrayList<GraphEdge>	edges;
	public ArrayList<Corner>	neighbors;
	public short				glID;
	public double				x, y;
	public double				alt				= Double.MAX_VALUE;
	public boolean				isWater			= false;
	public boolean				isCoast			= false;
	public double				weight			= 0;
	public Corner				downhillCorner	= null;
	public boolean checked = false;
	public float[] normal = {0.0f,0.0f,0.0f};

	public Corner() {
		edges = new ArrayList<GraphEdge>();
	}

	public Corner(double X, double Y) {
		this.x = X;
		this.y = Y;
	}

	public Corner(double X, double Y, double alt) {
		this.x = X;
		this.y = Y;
		this.alt = alt;
	}
	

	public void snapToGrid(double grid) {
		x = grid * Math.round(x/grid);
		y = grid * Math.round(y/grid);
	}

	public String toString() {
		String str = "||-  " + x + "," + y + "  -||";// + "," + Math.round(alt) + "  -||";
		return str;
	}

	public ArrayList<Corner> getNeighborCorners() {
		neighbors = new ArrayList<Corner>();
		for (int i = 0; i < edges.size(); i++) {
			if (edges.get(i).corner1 != this) {
				neighbors.add(edges.get(i).corner1);
			} else {
				neighbors.add(edges.get(i).corner2);
			}
		}
		return neighbors;
	}

	public Corner findLowestNeighbor() {
		Corner lowestCorner = null;
		double lowestAlt = Double.MAX_VALUE;

		for (int j = 0; j < neighbors.size(); j++) {
			Corner currCorner = neighbors.get(j);

			if (currCorner != null && currCorner != this && currCorner.alt < lowestAlt) {
				lowestAlt = currCorner.alt;
				lowestCorner = currCorner;
			}
		}
		return lowestCorner;
	}

	@Override
	public int compareTo(Object obj) {
		Corner otherCorner = (Corner) obj;
		if (otherCorner.alt > this.alt) {
			return -1;
		} else if (otherCorner.alt < this.alt) {
			return 1;
		} else {
			return 0;
		}
	}
}
